﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;

namespace NewLandOY20
{
    public partial class SetSerialForm : Form
    {
        public SetSerialForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 获取当前机器活动的串口，并添加到combobox选择项
        /// </summary>
        private void getSerialPort()
        {
            RegistryKey keyCom = Registry.LocalMachine.OpenSubKey("Hardware\\DeviceMap\\SerialComm");
            if (keyCom != null)
            {
                comboBox_port.Items.Clear();

                string[] sSubKeys = keyCom.GetValueNames();
                foreach (string sName in sSubKeys)
                {
                    string sValue = (string)keyCom.GetValue(sName);
                    comboBox_port.Items.Add(sValue);
                }
            }
        }

        /// <summary>
        /// 窗体load事件
        /// 获取活动的串口号
        /// 读取配置文件，并使用上次选择的串口信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SetForm_Load(object sender, EventArgs e)
        {
            getSerialPort();        // 获取端口号

            // 设置combobox的选择索引
            comboBox_port.Text = MyConfigHelper.getConfigValue(MyConstant.iConfigPort);
            comboBox_baud.Text = MyConfigHelper.getConfigValue(MyConstant.iConfigBaud);
            comboBox_data.Text = MyConfigHelper.getConfigValue(MyConstant.iConfigData);
            comboBox_check.SelectedIndex = Convert.ToInt16(MyConfigHelper.getConfigValue(MyConstant.iConfigCheck));
            comboBox_stop.Text = MyConfigHelper.getConfigValue(MyConstant.iConfigStop);
        }

        /// <summary>
        /// 确定按钮的点击事件
        /// 判断是否有未配置的选项
        /// 如果全部配置完毕，将配置保存在配置文件，将窗体的返回信息设置为OK，关闭窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (comboBox_port.SelectedIndex != -1 && comboBox_baud.SelectedIndex != -1 && comboBox_data.SelectedIndex != -1 && comboBox_check.SelectedIndex != -1 && comboBox_stop.SelectedIndex != -1)
            {
                MyConfigHelper.setConfigValue(MyConstant.iConfigPort, comboBox_port.Text);
                MyConfigHelper.setConfigValue(MyConstant.iConfigBaud, comboBox_baud.Text);
                MyConfigHelper.setConfigValue(MyConstant.iConfigData, comboBox_data.Text);
                MyConfigHelper.setConfigValue(MyConstant.iConfigCheck, comboBox_check.SelectedIndex.ToString());
                MyConfigHelper.setConfigValue(MyConstant.iConfigStop, comboBox_stop.Text);

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("存在未选择的选项！", "提示");
            }
        }

        /// <summary>
        /// 取消按钮的点击事件
        /// 设置窗体的返回值为Cancel，关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

    }
}
